package sys.transport;

import sys.message.*;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class MessageSenderSMTP implements MessageSender
{
    String server, port;

    public MessageSenderSMTP(String server, String port)
    {
        this.server = server;
        this.port = port;
    }

    public void send(UniMessage m) throws MessagingException
    {
        Properties props = new Properties();
        props.put("mail.smtp.host", server);
        props.put("mail.smtp.port", port);

        Session session = Session.getDefaultInstance(props, null);

        Message msg = new MimeMessage(session);

        InternetAddress addressFrom = new InternetAddress(m.getFrom());
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[1]; 
        addressTo[0] = new InternetAddress(m.getTo());
        msg.setRecipients(Message.RecipientType.TO, addressTo);
       
        msg.setSubject(m.getSubj());
        msg.setContent(m.getBody(), "text/plain");

        Transport.send(msg);
    }
}
